package com.zhledu.util;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class Command {
    public static String runCommand(String cmd, int tp) {
        StringBuffer buf = new StringBuffer(1000);
        String rt = "-1";
        try {
            Process pos = Runtime.getRuntime().exec(cmd);
            pos.waitFor();
            if (tp == 1) {
                if (pos.exitValue() == 0) {
                    rt = "1";
                }
            } else {
                InputStreamReader ir = new InputStreamReader(pos.getInputStream());
                LineNumberReader input = new LineNumberReader(ir);
                String ln = "";
                while ((ln = input.readLine()) != null) {
                    buf.append(ln + "<br>");
                }
                rt = buf.toString();
                input.close();
                ir.close();
            }
        } catch (java.io.IOException e) {
            rt = e.toString();
        } catch (Exception e) {
            rt = e.toString();
        }
        return rt;
    }

    public static void main(String[] args) {
        runCommand("docker run -d --name u1 -p 8080:80 dorowu/ubuntu-desktop-lxde-vnc:bionic",0);
       /* //runCommand("docker stop u1",0);
        runCommand("docker start u1",0);*/
    }
}
